//
//  MRNestMainViewController.m
//  MRDemo
//
//  Created by dingping on 2020/8/18.
//  Copyright © 2020 dingping. All rights reserved.
//

#import "MRNestMainViewController.h"
#import <TYPagerController/TYPagerController.h>
#import <TYPagerController/TYTabPagerBar.h>
#import "MRScrollView.h"
#import "SDCycleScrollView.h"
#import "YYKit.h"

#import "MRNestContentViewController.h"
#import "MRContentOtherViewController.h"

@interface MRNestMainViewController ()<
UIScrollViewDelegate,
TYTabPagerBarDataSource,
TYTabPagerBarDelegate,
TYPagerControllerDataSource,
TYPagerControllerDelegate,
UIScrollViewDelegate,
MRNestContentVCDelegate
>

@property (nonatomic, strong) SDCycleScrollView *bannerView;

@property (nonatomic, weak) TYTabPagerBar *tabBar;
@property (nonatomic, weak) TYPagerController *pagerController;

@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, assign) NSInteger currentIndex;

@property (nonatomic, strong) MRScrollView *mainScrollView;
@property (nonatomic, strong) UIScrollView *currentScrollView;

@property (nonatomic, assign) CGFloat lastOffsetY;



@end

@implementation MRNestMainViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"scrollView嵌套";
    
    self.edgesForExtendedLayout = UIRectEdgeBottom;
    
    [self.view addSubview:self.mainScrollView];
    [self.mainScrollView addSubview:self.bannerView];
    
    [self addTabPageBar];
    [self addPagerController];
    [self loadCateData];
}


- (void)dealloc
{
    NSLog(@"main dealloc");
}

- (void)loadCateData
{
    self.titleArray = @[@"测试一",@"测试二",@"测试三",@"测试四",@"测试五",@"测试六"];
    [self.tabBar reloadData];
}


#pragma mark - ScrollViewDelegate -
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat hoverY = self.tabBar.origin.y;
    if (self.currentScrollView && self.currentScrollView.contentOffset.y > 0) {
        scrollView.contentOffset = CGPointMake(0, hoverY);
    }
    if (scrollView.contentOffset.y < hoverY) {
        if(self.currentScrollView.contentOffset.y > 0) {
            self.currentScrollView.contentOffset = CGPointZero;
        }
    }
}

- (void)subScrollViewDidScroll:(UIScrollView *)scrollView
{
    self.currentScrollView = scrollView;
    
    CGFloat hoverY = self.tabBar.origin.y;;
    
    BOOL shouldProcess = YES;
    if (scrollView.contentOffset.y <= self.lastOffsetY) {
        if(self.mainScrollView.contentOffset.y == 0) {
            shouldProcess = NO;
        }
        else {
            if(self.mainScrollView.contentOffset.y < hoverY) { //保证非悬浮状态滚动时 offsetY=0;
                scrollView.contentOffset = CGPointZero;
            }
        }
    }
    
    if (shouldProcess) {
        if (self.mainScrollView.contentOffset.y < hoverY) {
            if (scrollView.contentOffset.y > 0) {
                scrollView.contentOffset = CGPointZero;
            }
        }
        else {
            self.mainScrollView.contentOffset = CGPointMake(0, hoverY);
        }
    }
    
    self.lastOffsetY = scrollView.contentOffset.y;
}




#pragma mark - UI -
- (void)addTabPageBar
{
    TYTabPagerBar *tabBar = [[TYTabPagerBar alloc] initWithFrame:CGRectMake(0, 210, kScreenW, 45)];
    tabBar.backgroundColor = [UIColor whiteColor];
    
    tabBar.layout.barStyle = TYPagerBarStyleProgressElasticView;
    tabBar.layout.progressColor = [UIColor orangeColor];
    tabBar.layout.progressHeight = 4;
    tabBar.layout.progressWidth = 28;
    tabBar.layout.progressRadius = 2;
    tabBar.layout.cellWidth = kScreenW/5;
    tabBar.layout.cellEdging = 0;
    
    tabBar.layout.normalTextColor = [UIColor grayColor];
    tabBar.layout.selectedTextColor = [UIColor orangeColor];
    
    tabBar.layout.normalTextFont = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
    tabBar.layout.selectedTextFont = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
    
    tabBar.dataSource = self;
    tabBar.delegate = self;
    
    [tabBar registerClass:[TYTabPagerBarCell class] forCellWithReuseIdentifier:[TYTabPagerBarCell cellIdentifier]];
    
    _tabBar = tabBar;
    
    [self.mainScrollView addSubview:tabBar];
}

- (void)addPagerController
{
    CGFloat controllerOriginY = CGRectGetMaxY(self.tabBar.frame);
    
    TYPagerController *pagerController = [[TYPagerController alloc] init];
    pagerController.view.frame = CGRectMake(0, controllerOriginY, kScreenW, kScreenH-45);
    
    pagerController.layout.prefetchItemCount = 1;
    pagerController.scrollView.bounces = NO;
    pagerController.scrollView.delaysContentTouches = NO;
    pagerController.scrollView.canCancelContentTouches = YES;
    
    pagerController.layout.addVisibleItemOnlyWhenScrollAnimatedEnd = YES;
    pagerController.dataSource = self;
    pagerController.delegate = self;
    pagerController.view.backgroundColor = [UIColor whiteColor];
    [self addChildViewController:pagerController];
    [self.mainScrollView addSubview:pagerController.view];
    _pagerController = pagerController;
    
}


- (MRScrollView *)mainScrollView {
    if(!_mainScrollView) {
        _mainScrollView = [[MRScrollView alloc]initWithFrame:self.view.bounds];
        _mainScrollView.showsVerticalScrollIndicator = NO;
        _mainScrollView.showsHorizontalScrollIndicator = NO;
        _mainScrollView.backgroundColor = [UIColor whiteColor];;
        _mainScrollView.delaysContentTouches = NO;
        _mainScrollView.canCancelContentTouches = YES;
        _mainScrollView.panGestureRecognizer.cancelsTouchesInView = NO;
        _mainScrollView.bounces = NO;
        _mainScrollView.delegate = self;
        _mainScrollView.scrollEnabled = YES;
        [_mainScrollView setContentSize:CGSizeMake(kScreenW , kScreenH*2)];
        if (@available(iOS 11.0, *)) {
            _mainScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
    }
    return _mainScrollView;
}


- (SDCycleScrollView *)bannerView {
    if(!_bannerView) {
        _bannerView = [[SDCycleScrollView alloc]initWithFrame:CGRectMake(15, 15, kScreenW-30, 180)];
        _bannerView.showPageControl = YES;
        _bannerView.backgroundColor = [UIColor clearColor];
        _bannerView.layer.cornerRadius = 6;
        _bannerView.layer.masksToBounds = YES;
        _bannerView.contentMode = UIViewContentModeScaleAspectFill;
        
        NSArray *imageNameArray = @[@"timg",@"timg",@"timg"];
        _bannerView.localizationImageNamesGroup = imageNameArray;
    }
    return _bannerView;
}

#pragma mark TYTabPagerBarDataSource
- (NSInteger)numberOfItemsInPagerTabBar {
    return self.titleArray.count;
}

- (UICollectionViewCell<TYTabPagerBarCellProtocol> *)pagerTabBar:(TYTabPagerBar *)pagerTabBar cellForItemAtIndex:(NSInteger)index
{
    UICollectionViewCell<TYTabPagerBarCellProtocol> *cell = [pagerTabBar dequeueReusableCellWithReuseIdentifier:[TYTabPagerBarCell cellIdentifier] forIndex:index];
    cell.titleLabel.text = self.titleArray[index];
    return cell;
}

#pragma mark  TYTabPagerBarDelegate
- (CGFloat)pagerTabBar:(TYTabPagerBar *)pagerTabBar widthForItemAtIndex:(NSInteger)index
{
    NSString *title = self.titleArray[index];
    return [pagerTabBar cellWidthForTitle:title];
}

- (void)pagerTabBar:(TYTabPagerBar *)pagerTabBar didSelectItemAtIndex:(NSInteger)index
{
    [_pagerController scrollToControllerAtIndex:index animate:YES];
}


#pragma mark  TYPagerControllerDataSource
- (NSInteger)numberOfControllersInPagerController {
    return self.titleArray.count;
}

- (UIViewController *)pagerController:(TYPagerController *)pagerController controllerForIndex:(NSInteger)index prefetching:(BOOL)prefetching
{
    MRNestBaseViewController *vc;
    if(index %2 == 0) {
        vc = [[MRContentOtherViewController alloc]init];
    }
    else {
        vc = [[MRNestContentViewController alloc]init];
    }
    
    vc.delegate = self;
    
    return vc;
}

#pragma mark  TYPagerControllerDelegate
- (void)pagerController:(TYPagerController *)pagerController transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex animated:(BOOL)animated
{
    [_tabBar scrollToItemFromIndex:fromIndex toIndex:toIndex animate:animated];
    if(fromIndex<0 || toIndex<0){
        return;
    }
    self.currentIndex = toIndex;
}

- (void)pagerController:(TYPagerController *)pagerController transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex progress:(CGFloat)progress
{
    [_tabBar scrollToItemFromIndex:fromIndex toIndex:toIndex progress:progress];
}

- (void)pagerControllerWillBeginScrolling:(TYPagerController *)pagerController animate:(BOOL)animate
{
    self.mainScrollView.scrollEnabled = NO;
}

- (void)pagerControllerDidEndScrolling:(TYPagerController *)pagerController animate:(BOOL)animate
{
    self.mainScrollView.scrollEnabled = YES;
}

- (void)pagerControllerDidScroll:(TYPagerController *)pagerController{}


@end
